Skip to content

Conversation

@Enna1
Copy link
Contributor

@Enna1 Enna1 commented Sep 11, 2025

The foldDeadPhiWeb function identifies and removes small dead PHI webs, it bails out if the web size exceeds threshold (16).

In the current implementation, when there is a phi node has large number of users that most of them are phi nodes, we still push them on the Stack even if the number of phi nodes user exceeds the threshold.

This patch checks the early stop condition when we push an unvisited phi node on the Stack.
With this change, the wall duration of total instcombine pass decreased from 523,649.276 ms to 208,687.042 ms in an our internal case.

The foldDeadPhiWeb function identifies and removes small dead PHI webs, it bails
out if the web size exceeds threshold (16).

In the current implementation, when there is a phi node has large number of
users that most of them are phi nodes, we still push them on the `Stack` even
if the numbers of phi nodes user already exceed the threshold. It is unnecessary
and may consume too much time.

This patch checks the early stop condition when we push an unvisited phi node
on the `Stack`.
With this change, the wall duration of total instcombine pass decreased from
523,649.276 ms to 30,239.399 ms in an our internal case.
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Sep 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mingjie Xu (Enna1)

Changes

The foldDeadPhiWeb function identifies and removes small dead PHI webs, it bails out if the web size exceeds threshold (16).

In the current implementation, when there is a phi node has large number of users that most of them are phi nodes, we still push them on the Stack even if the number of phi nodes user exceeds the threshold.

This patch checks the early stop condition when we push an unvisited phi node on the Stack.
With this change, the wall duration of total instcombine pass decreased from 523,649.276 ms to 30,239.399 ms in an our internal case.


Full diff: https://github.com/llvm/llvm-project/pull/158057.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp (+7-6)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ed9a0be6981fa..c3643a7cb4c08 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -62,14 +62,15 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) {
   Stack.push_back(&PN);
   while (!Stack.empty()) {
     PHINode *Phi = Stack.pop_back_val();
-    if (!Visited.insert(Phi).second)
-      continue;
-    // Early stop if the set of PHIs is large
-    if (Visited.size() == 16)
-      return false;
     for (User *Use : Phi->users()) {
-      if (PHINode *PhiUse = dyn_cast<PHINode>(Use))
+      if (PHINode *PhiUse = dyn_cast<PHINode>(Use)) {
+        if (!Visited.insert(Phi).second)
+          continue;
+        // Early stop if the set of PHIs is large
+        if (Visited.size() >= 16)
+          return false;
         Stack.push_back(PhiUse);
+      }
       else
         return false;
     }

@github-actions
Copy link

github-actions bot commented Sep 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@nikic
Copy link
Contributor

nikic commented Sep 11, 2025

for (User *Use : Phi->users()) {
if (PHINode *PhiUse = dyn_cast<PHINode>(Use))
if (PHINode *PhiUse = dyn_cast<PHINode>(Use)) {
if (!Visited.insert(Phi).second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should insert PhiUse, not Phi.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.. Fixed

bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) {
SmallVector<PHINode *, 16> Stack;
SmallPtrSet<PHINode *, 16> Visited;
Stack.push_back(&PN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add the initial phi node to the Visited set.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Enna1
Copy link
Contributor Author

Enna1 commented Sep 15, 2025

Thanks for your kind review.
(Sorry for my dumb mistake...)

@Enna1 Enna1 merged commit 5850c44 into main Sep 15, 2025
9 checks passed
@Enna1 Enna1 deleted the users/Enna1/perf/fold-dead-phi-web-opt-time branch September 15, 2025 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants